package com.shire.shire_interface.controller.invite;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.invite.WxInviteSubTask;
import com.shire.shire_interface.model.invite.WxInviteSubTaskExample;
import com.shire.shire_interface.model.invite.WxInviteTask;
import com.shire.shire_interface.service.friend.WxFriendService;
import com.shire.shire_interface.service.invite.WxInviteSubTaskService;
import com.shire.shire_interface.service.invite.WxInviteTaskService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

//@Api(tags = "加好友 添加好友/添加群好友")
@RestController
@RequestMapping("/friend")
public class WxInviteTaskController extends BaseController {

    @Resource
    private WxInviteTaskService wxInviteTaskService;
    @Resource
    private WxInviteSubTaskService wxInviteSubTaskService;
    @Resource
    private WxFriendService wxFriendService;


//    @ApiOperation(value = "添加邀请好友任务 csid, wxaccountid, friendList, groupFriend：[{ \"groupNo\": xxx, \"friendWxAccount\": xxx}, ...] ， type 0：手机号添加 1：微信号添加 2：添加群成员")
//    @ApiImplicitParams(value = {
//            @ApiImplicitParam(paramType = "body", dataType = "WxInviteTask", name = "wxInviteTask")})
//    @ApiResponses({@ApiResponse(code = 200, message = "调用成功返回", response = Response.class)})
    @PostMapping(value = "/inviteFriend")
    @ResponseBody
    public Response inviteFriend(@RequestBody WxInviteTask wxInviteTask, HttpSession session) {
        Response response = new Response();
        try {
            Integer csid = wxInviteTask.getCsid();
            if (!checkLoginUserByCsId(session, csid)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            String wxaccountid = wxInviteTask.getWxaccountid();
            List<String> friendList = wxInviteTask.getFriendList();
            List<JSONObject> groupFriend = wxInviteTask.getGroupFriend();
            Integer type = wxInviteTask.getType();
            wxInviteTask.setAddcount(0);
            wxInviteTask.setSenditemcount(0);

            wxInviteTask.setCreatetime(new Date());

            wxInviteTaskService.inviteFriend(wxInviteTask);

            WxInviteSubTask wxInviteSubTask = new WxInviteSubTask();

            wxInviteSubTask.setWxaccount(wxInviteTask.getWxaccountid());
            wxInviteSubTask.setCsid(wxInviteTask.getCsid());
            wxInviteSubTask.setTaskid(wxInviteTask.getId());
            wxInviteSubTask.setCreatetime(new Date());

            if (!type.equals(2)) {
                for (String friend : friendList) {
                    wxInviteSubTask.setWxinviteeaccount(friend);
                    wxInviteSubTask.setId(null);
                    wxInviteSubTaskService.insert(wxInviteSubTask);
                }
            }else {
                for (JSONObject jsonObject : groupFriend) {

                    String groupNo = jsonObject.getString("groupNo");
                    String friendWxAccount = jsonObject.getString("friendWxAccount");

                    wxInviteSubTask.setWxgroup(groupNo);
                    wxInviteSubTask.setWxinviteeaccount(friendWxAccount);
                    wxInviteSubTask.setGroupavatar(jsonObject.getString("groupAvatar"));
                    wxInviteSubTask.setGroupname(jsonObject.getString("groupName"));
                    wxInviteSubTask.setFriendwxid(jsonObject.getString("friendWxId"));
                    wxInviteSubTask.setFriendnick(jsonObject.getString("friendNick"));
                    wxInviteSubTask.setFriendavatar(jsonObject.getString("friendAvatar"));
                    wxInviteSubTask.setId(null);
                    wxInviteSubTaskService.insert(wxInviteSubTask);
                }
            }

            if (!type.equals(2)){
                Integer id = wxInviteTask.getId();
                WxInviteSubTaskExample wxInviteSubTaskExample = new WxInviteSubTaskExample();
                wxInviteSubTaskExample.createCriteria().andCsidEqualTo(csid).andWxaccountEqualTo(wxaccountid).andTaskidEqualTo(id);
                List<WxInviteSubTask> inviteHistorys = wxInviteSubTaskService.getInviteHistory(wxInviteSubTaskExample);

                wxInviteTask.setWxInviteSubTaskList(inviteHistorys);

                response.setData(wxInviteTask);
            }

            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }


//    @ApiOperation(value = "取消任务 id")
    @ResponseBody
    @PostMapping("/cancel")
    public Response cancel(@RequestBody JSONObject jsonObject, HttpSession session) {
        Response response = new Response();
        try {
            if (!checkLoginUser(session)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            Integer id = jsonObject.getInteger("id");
            WxInviteTask wxInviteTask = new WxInviteTask();
            wxInviteTask.setId(id);
            wxInviteTask.setTaskstatus(1);
            wxInviteTaskService.updateBySelective(wxInviteTask);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }

        return response;
    }
}
